﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GZSolver
{
    /// <summary>
    /// Provides methods for logging results.
    /// </summary>
    class ResultsWriter
    {
        [Flags]
        public enum SourceOutput
        {
            CONSOLE = 1,
            FILE = 2
        }

        /// <summary>
        /// Create new stream writer to override previous results.
        /// </summary>
        private static StreamWriter sw = new StreamWriter("result.txt");

        public static void PrintResult(LinearSystemSolvingResults result, SourceOutput outSource)
        {
            if ((outSource & SourceOutput.CONSOLE) == SourceOutput.CONSOLE)
            {
                ResultsWriter.WriteResult(Console.Out, result);
            }
            if ((outSource & SourceOutput.FILE) == SourceOutput.FILE)
            {
                if (sw == null)
                {
                    sw = new StreamWriter("result.txt", true);
                }
                ResultsWriter.WriteResult(ResultsWriter.sw, result);
                sw.Close();
                sw = null;
            }
        }

        private static void WriteResult(TextWriter tw, LinearSystemSolvingResults result)
        {
            for (int i = 0; i < result.ResultVector.Length; i++)
            {
                tw.WriteLine("X{0}: {1:0.00000000}", i, result.ResultVector[i]);
            }
            tw.WriteLine("Elapsed ticks: {0}", result.ElapsedTicks);
        }
    }
}
